$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        licenceDevList: null,
        type: null,
        deviceId: null,
        status: null,
        iId: 0,
        init: function () {
            this.licenceDevList = new xhDataGrid($('#devLicenceList'), 'licence/getLicenceDev');
            this.licenceDevList.parseToolbar().init();
            xhExtra.disableButton($('#bt_list_active'), 'eu');
            xhExtra.disableButton($('#bt_list_unbind'), 'eu');
            this.listener();
        },
        listener: function () {
            var $this = this;
            //搜索
            $('#bt_search').click(function () {
                $this.type = $('#licenceType').val();
                $this.status = $('#licenceStatus').val();
                $this.deviceId = $('#deviceId').val();
                $this.funObj().searchList();
            });
            $this.licenceDevList.onCheck = function (rowIndex, rowData) {
                if ($this.iId == rowData['id']) {
                    $this.iId = 0;
                    $this.licenceDevList.command('unselectRow', rowIndex);
                    xhExtra.disableButton($('#bt_list_active'), 'eu');
                    xhExtra.disableButton($('#bt_list_unbind'), 'eu');
                    return false;
                } else {
                    $this.iId = rowData['id'];
                    xhExtra.enableButton($('#bt_list_active'), 'eu');
                    xhExtra.enableButton($('#bt_list_unbind'), 'eu');
                }
            };
            // $('#bt_list_active').click(function () {
            //     window.top.swal({
            //         showCancelButton: true,
            //         confirmButtonText: '确认',
            //         cancelButtonText: '取消',
            //         showLoaderOnConfirm: true,
            //         allowOutsideClick: false,
            //         title: '激活',
            //         text:'确认激活？',
            //         preConfirm: function() {
            //             return new Promise(function () {
            //                 $.ajax({
            //                     url: 'activeLicenceDev',
            //                     data: {id:$this.iId},
            //                     type: 'POST',
            //                     success: function (data) {
            //                         console.log(data);
            //                         window.top.xhTop.showSuccess(
            //                             data.msg
            //                         );
            //                         window.top.xhTop.getActiveFrame().obj.licenceDevList.reload();
            //                     },
            //                     error: function (data) {
            //                         console.log(data);
            //                         window.top.xhTop.showError(
            //                             data.responseJSON.error,
            //                             data.responseJSON.msg.msg,
            //                         );
            //                     }
            //                 });
            //             }).catch(swal.noop);
            //         }
            //     });
            //
            // });

            $('#bt_list_unbind').click(function () {
                var key = $this.licenceDevList.getSelectValue('key');
                var type = $this.licenceDevList.getSelectValue('type');
                window.top.swal({
                    showCancelButton: true,
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    showLoaderOnConfirm: true,
                    allowOutsideClick: false,
                    title: '解绑',
                    text:'确认解绑？',
                    preConfirm: function() {
                        return new Promise(function () {
                            $.ajax({
                                url: 'unbindLicenceDev',
                                data: {
                                    id:$this.iId,
                                    type:type,
                                    key:key,
                                    },
                                type: 'POST',
                                success: function (data) {
                                    window.top.xhTop.showSuccess(
                                        data.msg
                                    );
                                    window.top.xhTop.getActiveFrame().obj.licenceDevList.reload();
                                },
                                error: function (data) {
                                    window.top.xhTop.showError(
                                        data.responseJSON.error,
                                        data.responseJSON.msg.msg,
                                    );
                                }
                            });
                        }).catch(swal.noop);
                    }
                });

            });

        },
        funObj: function () {
            var $this = this;
            return {
                searchList: function () {
                    $this.licenceDevList.reload({type: $this.type, status: $this.status, deviceId: $this.deviceId});
                },
            }
        }
    }
};
